package com.yu.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.yu.bean.Dept;
import com.yu.bean.Employee;
import com.yu.service.DeptService;
import com.yu.service.EmployeeService;

@Controller
@RequestMapping("/employee")
public class EmployeeController {

	@Autowired
	private EmployeeService employeeService;

	@Autowired
	private DeptService deptService;

	@RequestMapping("/getAll")
	public String getAll(Model model) {
		List<Employee> list = employeeService.getAllWithDept();
		model.addAttribute("list", list);
		return "employee/showList";
	}

	@RequestMapping("/toAdd")
	public String toAdd(Model model) {
		List<Dept> list = deptService.getAll();
		model.addAttribute("deptList", list);
		return "employee/add";
	}

	@RequestMapping("/add")
	public String add(Employee employee) {
		employeeService.add(employee);
		return "forward:/employee/getAll.do";
	}

	@RequestMapping("/delete")
	public String delete(int id) {
		employeeService.delete(id);
		return "forward:/employee/getAll.do";
	}

	@RequestMapping("/toEdit")
	public String toEdit(Model model, int id) {
		Employee employee = employeeService.getById(id);
		model.addAttribute("employee", employee);
		List<Dept> list = deptService.getAll();
		model.addAttribute("deptList", list);
		return "employee/edit";
	}

	@RequestMapping("/edit")
	public String edit(Employee employee) {
		employeeService.update(employee);
		return "forward:/employee/getAll.do";
	}
}